package com.xywy.bloodandroid.act;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xywy.bloodandroid.MyApplication;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.broadcast.RemindService;
import com.xywy.bloodandroid.fragment.BloodFragment;
import com.xywy.bloodandroid.fragment.DoctorOnlineFragment;
import com.xywy.bloodandroid.fragment.PersonalFragment;

public class MainFragmentAct extends FragmentActivity implements
		OnClickListener {
	private FragmentManager fm;
	private LinearLayout blood_manger_lin;
	private TextView blood_manger_text;
	private ImageView blood_manger;
	private LinearLayout doctor_online_lin;
	private ImageView doctor_online;
	private TextView doctor_online_text;
	private LinearLayout person_center_line;
	private ImageView person_center;
	private TextView person_center_text;

	private FragmentTransaction fragmentTransaction;
	private DoctorOnlineFragment doctorFragement;
	private BloodFragment bloodFragement;
	private PersonalFragment personFragement;

	public static boolean CANTOUCH = true;

	private boolean isExit = false;
	/**
	 * 退出应用exitHandler
	 */
	private Handler exitHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {

			isExit = false;
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.act_mainfragmentact);

		MyApplication.getInstance().addActivity(this);

		initview();
		fm = getSupportFragmentManager();
		initclickLister();
		switchFragment(R.id.doctor_online_lin);
		startAlarm();
	}

	void startAlarm(){
		Intent intent = new Intent(this,RemindService.class);
		startService(intent);
	}

	private void initclickLister() {

		blood_manger_lin.setOnClickListener(this);
		doctor_online_lin.setOnClickListener(this);
		person_center_line.setOnClickListener(this);
	}

	private void initview() {

		blood_manger_lin = (LinearLayout) findViewById(R.id.blood_manger_lin);
		blood_manger = (ImageView) findViewById(R.id.blood_manger);
		blood_manger_text = (TextView) findViewById(R.id.blood_manger_text);

		doctor_online_lin = (LinearLayout) findViewById(R.id.doctor_online_lin);
		doctor_online = (ImageView) findViewById(R.id.doctor_online);
		doctor_online_text = (TextView) findViewById(R.id.doctor_online_text);

		person_center_line = (LinearLayout) findViewById(R.id.person_center_line);
		person_center = (ImageView) findViewById(R.id.person_center);
		person_center_text = (TextView) findViewById(R.id.person_center_text);
	}

	/**
	 * ״̬
	 * 
	 * @param id
	 */
	private void switchFragment(int id) {
		fragmentTransaction = fm.beginTransaction();
		//
		hideFragments(fragmentTransaction);
		int fragement_textcolor_press = MainFragmentAct.this.getResources()
				.getColor(R.color.fragement_text_press);
		int fragement_textcolor_normal = MainFragmentAct.this.getResources()
				.getColor(R.color.fragement_text_normal);
		switch (id) {

		case R.id.doctor_online_lin:

			blood_manger.setBackgroundResource(R.drawable.blood_manger_normal);
			blood_manger_text.setTextColor(fragement_textcolor_normal);

			doctor_online.setBackgroundResource(R.drawable.doctor_head_press);
			doctor_online_text.setTextColor(fragement_textcolor_press);

			person_center.setBackgroundResource(R.drawable.persional_normal);
			person_center_text.setTextColor(fragement_textcolor_normal);
			if (doctorFragement == null) {
				doctorFragement = new DoctorOnlineFragment();
				fragmentTransaction.add(R.id.content_frame, doctorFragement);
			} else {
				fragmentTransaction.show(doctorFragement);
			}
			break;

		case R.id.blood_manger_lin:

			blood_manger.setBackgroundResource(R.drawable.blood_manger_press);
			blood_manger_text.setTextColor(fragement_textcolor_press);

			doctor_online.setBackgroundResource(R.drawable.doctor_head_normal);
			doctor_online_text.setTextColor(fragement_textcolor_normal);

			person_center.setBackgroundResource(R.drawable.persional_normal);
			person_center_text.setTextColor(fragement_textcolor_normal);
			if (bloodFragement == null) {
				bloodFragement = new BloodFragment();
				fragmentTransaction.add(R.id.content_frame, bloodFragement);
			} else {
				fragmentTransaction.show(bloodFragement);
			}

			break;
		case R.id.person_center_line:

			blood_manger.setBackgroundResource(R.drawable.blood_manger_normal);
			blood_manger_text.setTextColor(fragement_textcolor_normal);

			doctor_online.setBackgroundResource(R.drawable.doctor_head_normal);
			doctor_online_text.setTextColor(fragement_textcolor_normal);

			person_center.setBackgroundResource(R.drawable.persional_press);
			person_center_text.setTextColor(fragement_textcolor_press);
			if (personFragement == null) {
				personFragement = new PersonalFragment();
				fragmentTransaction.add(R.id.content_frame, personFragement);
			} else {
				fragmentTransaction.show(personFragement);
			}
			break;
		}
		fragmentTransaction.commit();
	}

	@Override
	public void onBackPressed() {
		if (CANTOUCH)
			exit();
	}

	private void exit() {
		if (!isExit) {
			isExit = true;
			Toast.makeText(getApplicationContext(), "再按一次退出应用",
					Toast.LENGTH_SHORT).show();
			exitHandler.sendEmptyMessageDelayed(0, 2000);
		} else {
			MyApplication.getInstance().exit();
		}
	}

	/**
	 * 
	 * @param transaction
	 */
	private void hideFragments(FragmentTransaction ft) {
		if (bloodFragement != null) {
			ft.hide(bloodFragement);
		}
		if (doctorFragement != null) {
			ft.hide(doctorFragement);
		}
		if (personFragement != null) {
			ft.hide(personFragement);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.blood_manger_lin:

			break;
		case R.id.doctor_online_lin:

			break;
		case R.id.person_center_line:

			break;
		}
		switchFragment(v.getId());

	}

	public void thisFinish() {
		MainFragmentAct.this.finish();
	}
}
